/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-15
*/
package com.rzico.article.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.article.model.BillFareAffiliatedVo;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.article.entity.BillFareAffiliated;
import com.rzico.article.service.BillFareAffiliatedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 * 名菜关联店铺控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-12-15
 */
@Api(description = "名菜关联店铺接口")
@RestController
@RequestMapping("/admin/billFareAffiliated")
public class BillFareAffiliatedAdminController extends BaseController {

    @Autowired
    private BillFareAffiliatedService billFareAffiliatedService;

    /**
     * 分页查询名菜关联店铺
     *
     * @return
    */
    @ApiOperation("分页查询名菜关联店铺")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "billFareId", value = "菜谱ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shopName", value = "店铺名称", dataType = "String", paramType = "query")
    })
    public CommResult<BillFareAffiliatedVo> list(Long billFareId, String shopName, Pageable pageable) {

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<BillFareAffiliatedVo> list = billFareAffiliatedService.selectBillFareList(billFareId,shopName);
        PageResult<BillFareAffiliatedVo> pageResult = new PageResult<BillFareAffiliatedVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条名菜关联店铺
     *
     * @return
    */
    @ApiOperation("查询单条名菜关联店铺")
    @GetMapping("/find/{id}")
    public CommResult<BillFareAffiliated> find(@PathVariable String id) {
        BillFareAffiliated result = billFareAffiliatedService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("billFareAffiliated",result);
        return CommResult.success(data);

    }

    /**
     * 保存名菜关联店铺
     *
     * @param billFareAffiliated
     * @return
     */
    @ApiOperation("保存名菜关联店铺")
    @PostMapping("/save")
    public CommResult<BillFareAffiliated> save(@RequestBody BillFareAffiliated billFareAffiliated) {

        Map<String,Object> params = new HashMap<>();
        params.put("billFareId",billFareAffiliated.getBillFareId());
        params.put("shopId",billFareAffiliated.getShopId());
        Integer w = billFareAffiliatedService.selectRowCount(params);
        if (w>0) {
            return CommResult.error("不能重覆添加");
        }

        int affectCount = billFareAffiliatedService.insertUseGeneratedKeys(billFareAffiliated);
        if (affectCount <= 0) {
            return CommResult.error();
        }

        BillFareAffiliatedVo shopBillFare = billFareAffiliatedService.selectBillFare(billFareAffiliated.getBillFareId(),billFareAffiliated.getShopId());
        return CommResult.success(shopBillFare);

    }


    /**
     * 批量删除名菜关联店铺
     *

     * @return
     */
    @Log(desc = "批量删除名菜关联店铺", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除名菜关联店铺,ids用逗号拼接")
    @PostMapping("/del")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "billFareId", value = "菜谱ID", dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "shopId", value = "店铺ID", dataType = "String", paramType = "query")
    })
    public CommResult<BillFareAffiliated> del(Long billFareId,Long shopId) {
        BillFareAffiliated billFareAffiliated = new BillFareAffiliated();
        billFareAffiliated.setShopId(shopId);
        billFareAffiliated.setBillFareId(billFareId);
        billFareAffiliatedService.delete(billFareAffiliated);
        return CommResult.success();
    }

    /**
     * 更新名菜关联店铺
     *
     * @param billFareAffiliated
     * @return
     */
    @Log(desc = "更新名菜关联店铺", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新名菜关联店铺")
    @PostMapping("/update")
    public CommResult<BillFareAffiliated> update(@RequestBody BillFareAffiliated billFareAffiliated) {
        int affectCount = billFareAffiliatedService.updateByPrimaryKeySelective(billFareAffiliated);
        if (affectCount <= 0){
            return CommResult.error();
        }
        BillFareAffiliated result = billFareAffiliatedService.findById(billFareAffiliated.getId());
        return CommResult.success(result);
    }

}
